package gameserver;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;
import javax.swing.JTextArea;
//import java.util.Iterator;

public class Server extends Thread  {
	
	// general server information
	public String serverName = "MyServer";
	public int port = 8000;
	public int maxPlayers = 16;

        // data management
        public Queue<Data> events = new LinkedList<Data>(); // incoming messages from clients (EventInterpreter)
        public Queue<Data> output = new LinkedList<Data>(); // outgoing messages from

        public EventInterpreterThread eit = null;
        //public Queue<Data> events = new LinkedList<Data>(); //

        // server network / serverstate information
	public ServerSocket serverSocket = null; // the socket in which the server resides
	public ArrayList<Connection> clients = new ArrayList<Connection>(); // connected clients (threads)
	
        public int mode = SGSGlobal.OFF; // determines how the server is currently running
        public String status = ""; // status message for server modes / actions

        // -------------------------------------------------------

	public Server(int port, JTextArea CONSOLE) {
            this.port = port;
            eit = new EventInterpreterThread(this, CONSOLE);
        }
        public Server() {
            this(8000, null);
	}

        @Override
	public void start() {

            // initialize eventinterpreter for handling all game messages
            System.out.println("SSS");
            eit.start(); // eit is initialized in constructor, just need to start it now
            // wait until start command is received
            mode = SGSGlobal.OFF;
            while (mode == SGSGlobal.OFF) {
                // sit idle, maybe use a wait(x)?
            }

            // try to set up server
            try {
                serverSocket = new ServerSocket(port);
            } catch (Exception e) {
                mode = SGSGlobal.FATAL_ERROR;
                status = "Can't bind to socket.";
            }
            mode = SGSGlobal.ON;

            
            // start thread to accept new clients
            new ClientGreeterThread(this).start(); // start client greeter

            // start the game loop!
            while (mode == SGSGlobal.ON) {
                // wait for other threads
            }
            
            
            /*EventTimer et = new EventTimer();
            et.startTimer();
            
            while (!finished) { // remove this! put into thread? stalls program
                logic();
                if (et.getElapsedTimeSecs() > 3) {
                    System.out.println(SGSGlobal.getModeString(mode));
                    et.stopTimer();
                    et.startTimer();
                }
            }
            System.out.println("STARRT END");*/

            eit.guiWriteln("Server loop has ended.");

            System.out.println("HELLO");

            // cleanup - move this to the thread?
            /*try {
                if (serverSocket != null) {

                    // remove clients
                    Iterator<Connection> it = clients.iterator();
                    while (it.hasNext()) {
                        Connection c = it.next();
                        c.clientSocket.close();
                    }

                    // remove server
                    serverSocket.close();
                    serverSocket = null;
                    eventInterpreter.guiWriteln("Successfully ended.");
                } else {
                    eventInterpreter.guiWriteln("Server already down.");
                }
            } catch (Exception e) {
                //guiWriteln("Unable to shutdown server.");
            }*/
	}
	
	/** Update game state and handle game messages. */
	private void logic() {
		
            // read messages
            //EventTimer t = new EventTimer();
            //t.startTimer();
            /*while (!data.isEmpty()) { // only allow 1 second processing time
                eventInterpreter.processMessage(data.remove()); // remove message and process it
                //t.stopTimer();
                //t.startTimer();
            }*/

            // process game events
                // make enemies move

            // send data back to clients
                // send new changes to client (change in other players' x,y)
                // send server messages
		
	}
}
